// SPDX-License-Identifier: MIT
// Copyright 2020 NXP

/**
 * @file custom.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include <stdlib.h>
#include "lvgl.h"
#include "custom.h"
#include "cmsis_os2.h"
/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/
uint8_t toggle_mode = 0; // �л�ģʽ��0��ʾ�л�Сʱ��1��ʾ�л����ӣ�2��ʾ�л�����
uint8_t hour = 0;
uint8_t minute = 0;
uint8_t second = 0;
uint8_t temp = 0;
static lv_group_t * g;
lv_group_t *groupRect;
extern lv_indev_t * indev_keypad;
/**
 * Create a demo application
 */

void cb_timeout_periodic(void *arg) {
    (void)arg;
    char time_str[9];
    second++;
    if (second >= 60)
    {
        second = 0;
        minute++;
        if (minute >= 60)
        {
            minute = 0;
            hour++;
            if (hour >= 24)
            {
                hour = 0;
            }
        }
    }
    snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", hour, minute, second);
    lv_label_set_text(guider_ui.change_clock, time_str);

    printf("second:%d\n",second);
}

//初始化定时器，用于读秒
static void timer_init()
{
    osTimerId_t periodic_tid = osTimerNew(cb_timeout_periodic, osTimerPeriodic, NULL, NULL);
    osStatus_t status = osTimerStart(periodic_tid, 100);
}

static void screen_con_change_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_PRESSED:
    {

        // ��ȡ��ǰ��ʱ��ֵ
        char *time_str = lv_label_get_text(guider_ui.change_clock);
        hour = atoi(time_str);
        minute = atoi(time_str + 3);
        second = atoi(time_str + 6);
        // printf("%d", second);

        // �����л�ģʽ�����л�
        // printf("%d", hour);

        toggle_mode++;
        if (temp == 1)
        {
            temp = 0;
            toggle_mode = 0;
        }
        if (toggle_mode == 2)
        {
            temp = 1;
        }
        printf("%d\n", toggle_mode);
    }
    break;
    default:
        break;
    }
}
static void screen_con_minus_event_handler(lv_event_t *e)
{
    printf("con_minus_event:%d\n",e);
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_PRESSED:
    {
        // printf("remeber:%d", toggle_mode);
        if (toggle_mode == 0)
        {
            hour++;
            if (hour >= 24)
            {
                hour = 0;
            }
        }
        else if (toggle_mode == 1)
        {
            minute++;
            if (minute >= 60)
            {
                minute = 0;
                hour++;
                if (hour >= 24)
                {
                    hour = 0;
                }
            }
        }
        else if (toggle_mode == 2)
        {
            second++;
            // printf("second:%d",second);
            if (second >= 60)
            {
                second = 0;
                minute++;
                if (minute >= 60)
                {
                    minute = 0;
                    hour++;
                    if (hour >= 24)
                    {
                        hour = 0;
                    }
                }
            }
        }
        // printf("%d", hour);
        // printf("%d", minute);
        // printf("%d\n", second);
    }
    break;
    default:
        break;
    }
    char time_str[9];
    snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", hour, minute, second);
    lv_label_set_text(guider_ui.change_clock, time_str);
}

static void screen_con_plus_event_handler(lv_event_t *e)
{
    printf("con_plus_event:%d\n",e);
    lv_event_code_t code = lv_event_get_code(e);
    switch (code)
    {
    case LV_EVENT_PRESSED:
    {
        if (toggle_mode == 0)
        {
            if (hour == 0)
            {
                hour = 23;
            }
            else
            {
                hour--;
            }
        }
        else if (toggle_mode == 1)
        {
            if (minute == 0)
            {
                minute = 59;
                if (hour == 0)
                {
                    hour = 23;
                }
                else
                {
                    hour--;
                }
            }
            else
            {
                minute--;
            }
        }
        else if (toggle_mode == 2)
        {
            if (second == 0)
            {
                second = 59;
                if (minute == 0)
                {
                    minute = 59;
                    if (hour == 0)
                    {
                        hour = 23;
                    }
                    else
                    {
                        hour--;
                    }
                }
                else
                {
                    minute--;
                }
            }
            else
            {
                second--;
            }
        }
        // printf("%d", hour);
        // printf("%d", minute);
        // printf("%d\n", second);
    }
    break;
    default:
        break;
    }
    char time_str[9];
    snprintf(time_str, sizeof(time_str), "%02d:%02d:%02d", hour, minute, second);
    lv_label_set_text(guider_ui.change_clock, time_str);
}

void custom_init(lv_ui *ui)
{
    lv_obj_add_event_cb(ui->change_change, screen_con_change_event_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(ui->change_minus, screen_con_minus_event_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_event_cb(ui->change_plus, screen_con_plus_event_handler, LV_EVENT_ALL, NULL);
    timer_init();
    // groupRect = lv_group_create();


    // lv_group_add_obj(groupRect,ui->change_change);
    // lv_group_add_obj(groupRect,ui->change_minus);
    // lv_group_add_obj(groupRect,ui->change_plus);
    
    // lv_indev_set_group(indev_keypad, groupRect);
    // lv_obj_add_event_cb(ui->change_change, keyboard_handler, LV_EVENT_ALL, NULL);
    // lv_obj_add_event_cb(ui->change_minus, keyboard_handler, LV_EVENT_ALL, NULL);
    // lv_obj_add_event_cb(ui->change_plus, keyboard_handler, LV_EVENT_ALL, NULL);

}


